﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace Solidi
{
	public class DependencyKey : IDependencyKey
	{
		private Type _type;
		private string _name;

		public DependencyKey(Type type, string name)
		{
			_type = type;
			_name = name;
		}

		public DependencyKey(Type type)
		{
			_type = type;
		}

		public DependencyKey(IDependencyKey key, string name)
		{
			_type = key.Type;
			_name = name;
		}

		public DependencyKey(IDependencyKey key, Type type)
		{
			_type = type;
			_name = key.Name;
		}

		public override bool Equals(object obj)
		{
			DependencyKey key = obj as DependencyKey;

			if (key != null)
			{
				return _type == key._type && String.Equals(_name, key._name, StringComparison.Ordinal);
			}

			return base.Equals(obj);
		}

		public override int GetHashCode()
		{
			int hashCode = _type.GetHashCode();

			if (_name != null)
			{
				hashCode^= _name.GetHashCode();
			}

			return hashCode;
		}

		public Type Type
		{
			get
			{
				return _type;
			}
		}

		public string Name
		{
			get
			{
				return _name;
			}
		}

		public override string ToString()
		{
			if (!String.IsNullOrEmpty(_name))
			{
				return String.Format("type {0} named {1}", _type, _name);
			}

			return String.Format("type {0}", _type);
		}
	}
}
